﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aga.Controls.Tree;
using System.IO;
using System.Reflection;

namespace UnrealDebugger.Panels
{
    public partial class BreakpointsPanel : Panel
    {
        public class BreakpointNode : Node
        {
            public BreakpointNode()
                : base()
            {
            }

            public BreakpointNode(string text)
                : base(text)
            {
            }

            public string ClassName { get { return Breakpoint.ClassName; } }
            public int LineNo { get { return Breakpoint.LineNo; } }
            public Image Icon { get { return Breakpoint.Enabled ? _enabledBreakpoint : _disabledBreakpoint; } }
            public Breakpoint Breakpoint { get; set; }            

            public bool Enabled 
            { 
                get 
                { 
                    return Breakpoint.Enabled; 
                } 
                
                set 
                {
                    bool result = false;
                    if (value) { result = UnrealDebuggerIDE.Commands.ConnectBreakpoint(Breakpoint.ClassName, Breakpoint.LineNo); }
                    else { result = UnrealDebuggerIDE.Commands.DisconnectBreakpoint(Breakpoint.ClassName, Breakpoint.LineNo); }

                    if (result)
                    {
                        UnrealDebuggerIDE.Actions.EnableBreakpoint(Breakpoint.ClassName, Breakpoint.LineNo, value);
                    }
                } 
            }

            private static Image _enabledBreakpoint = null;
            private static Image _disabledBreakpoint = null;

            static BreakpointNode()
            {
                Stream file = null;
                Assembly assembly = typeof(Breakpoint).Assembly;

                file = assembly.GetManifestResourceStream("UnrealDebugger.Resources.EnabledBreakpoint.png");
                _enabledBreakpoint = Image.FromStream(file);

                file = assembly.GetManifestResourceStream("UnrealDebugger.Resources.DisabledBreakpoint.png");
                _disabledBreakpoint = Image.FromStream(file);
            }
        }

        private TreeModel _model = new TreeModel();

        public BreakpointsPanel()
            : base()
        {
            InitializeComponent();
            this.Load += new EventHandler(OnLoad);
            UnrealDebuggerIDE.Instance.OnBreakpointsChanged += new UnrealDebuggerIDE.BreakpointsChangedDelegate(OnBreakpointsChanged);
        }

        void OnLoad(object sender, EventArgs e)
        {
            this.treeViewBreakpoints.Model = _model;
        }

        #region Debugger Interface

        public void Clear()
        {
            this.SuspendLayout();
            this._model.Nodes.Clear();
            this.ResumeLayout();
        }

        public void OnBreakpointsChanged(MultiMap<string, Breakpoint> breakpoints)
        {
            this.SuspendLayout();
            this.treeViewBreakpoints.BeginUpdate();
            this._model.Nodes.Clear();

            foreach (string key in breakpoints.Keys)
            {
                foreach (Breakpoint breakpoint in breakpoints[key])
                {
                    BreakpointNode node = new BreakpointNode(breakpoint.ClassName);
                    node.Breakpoint = breakpoint;
                    this._model.Nodes.Add(node);
                }
            }

            this.treeViewBreakpoints.EndUpdate();
            this.ResumeLayout();
        }

        #endregion

        private void treeViewBreakpoints_DoubleClick(object sender, EventArgs e)
        {
            foreach (TreeNodeAdv node in this.treeViewBreakpoints.SelectedNodes)
            {
                BreakpointNode breakpointNode = node.Tag as BreakpointNode;
                Breakpoint breakpoint = breakpointNode != null ? breakpointNode.Breakpoint : null;

                if (breakpoint != null)
                {
                    Document doc = UnrealDebuggerIDE.Instance.GetDocumentFromClassname(breakpoint.ClassName);

                    if (doc != null)
                    {
                        UnrealDebuggerIDE.Instance.ShowDocument(doc);
                        doc.GotoLine(breakpoint.LineNo, 0);
                    }
                }
            }
        }

        private void toolStripButtonEnableAll_Click(object sender, EventArgs e)
        {
            foreach (TreeNodeAdv node in this.treeViewBreakpoints.AllNodes.ToArray())
            {
                BreakpointNode breakpointNode = node.Tag as BreakpointNode;
                if (breakpointNode != null) { breakpointNode.Enabled = true; }
            }
        }

        private void toolStripButtonDisableAll_Click(object sender, EventArgs e)
        {
            foreach (TreeNodeAdv node in this.treeViewBreakpoints.AllNodes.ToArray())
            {
                BreakpointNode breakpointNode = node.Tag as BreakpointNode;
                if (breakpointNode != null) { breakpointNode.Enabled = false; }
            }
        }

        private void toolStripButtonClearAll_Click(object sender, EventArgs e)
        {
            foreach (TreeNodeAdv node in this.treeViewBreakpoints.AllNodes.ToArray())
            {
                BreakpointNode breakpointNode = node.Tag as BreakpointNode;
                if (breakpointNode != null) { breakpointNode.Enabled = false; }
            }

            UnrealDebuggerIDE.Instance.Project.ClearBreakpoints();
        }
    }
}
